var request = require('request');
var ppomelo = require('../../../coinlib/ppomelo');
var coinpush = require('../../../coinlib/coinpush')

module.exports = function(app) {
  return new Handler(app);
};

var Handler = function(app) {
  this.app = app;
};



function pairlist(app, uid, para)  
{
	var ret = {
		"type":"pairlist", 
		"data":[
			{
				"baseCurrency":"btc", 
				"quoteCurrency":"eth",
				"increase":"88", 
				"lastPrice":"8.8888"   
			},
			{
				"baseCurrency":"btc", 
				"quoteCurrency":"usdt",
				"increase":"-88",
				"lastPrice":"88888"    
			}
		]
	};
	var c = function() {
		request("http://40.81.29.216:8130/pairlist/eth",
			function(error, response, body) {
				if(!error && response.statusCode == 200) {
					console.log(body) 
					var f = JSON.parse(body)
					ppomelo.Msg(app, "rolelist", uid, "dataMessage", f);
				}
			})
	}
	coinpush.AddTimer(uid , "pairlist", setInterval(c, 3 * 1000));
}
function coininfo(app, uid, para)  
{
	var ret = {
		"type":"coininfo", 
		"data":{
			"baseCurrency":"eth", 
			"quoteCurrency":"btc",
			"rmbPrice":"88",    //人民币价格   
			"increase":"88.888",   //涨幅
			"minPrice":"88", 		//分钟均价
			"highPrice24":"8888",   //15分钟最高价
			"lowPrice24":"88888",   //15分钟最低价
			"count24":"8",          //15分钟交易数量
			"newPrice":"8888"        //#最新成交价格
		}	
	};
	var c = function() {
		
		request("http://40.81.29.216:8130/coininfo/eth",
			function(error, response, body) {
				if(!error && response.statusCode == 200) {
					console.log(body) 
					var f = JSON.parse(body)
					ppomelo.Msg(app, "rolelist", uid, "dataMessage", f);
				}
			})
	}
	coinpush.AddTimer(uid , "coininfo", setInterval(c, 3 * 1000));

}

function bargraph(app, uid, para)  
{
	var ret = {
		"type":"bargraph", 
		"data":{
			"lasttime":8900,
			"data":{
				"8900":"8888",
				"8840":"8888",
				"8780":"8888",
				"8720":"8888",
				"8660":"8888",
				"8660":"8888",				
				"8540":"8888",
				"8480":"8888",
				"8420":"8888",
				"8360":"8888",
				"8300":"8888",
				"8240":"8888",
				"8180":"8888",
				"8120":"8888"
			}
		}
	};
	var c = function() {
		
		request("http://40.81.29.216:8130/bargraph/eth/btc",
			function(error, response, body) {
				if(!error && response.statusCode == 200) {
					console.log(body) 
					var f = JSON.parse(body)
					ppomelo.Msg(app, "rolelist", uid, "dataMessage", f);
				}
			})
	}
	coinpush.AddTimer(uid , "bargraph", setInterval(c, 5 * 1000));

}
function transdepth(app, uid, para)  
{
	var ret = {
		"type":"transdepth", 
		"data":{
			"buy":{
				"88":{	
					"count":"88", 
					"value":"8888",       
					"process":"8"
				},
				"89":{	
					"count":"88", 
					"value":"8888",       
					"process":"8"
				},
				"90":{	
					"count":"88", 
					"value":"8888",       
					"process":"8"
				}
			}, 
			"sell":{
				"88":{	
					"count":"88", 
					"value":"8888",       
					"process":"8"
				},
				"89":{	
					"count":"88", 
					"value":"8888",       
					"process":"8"
				},
				"90":{	
					"count":"88", 
					"value":"8888",       
					"process":"8"
				}
			}
		}
	}
	var c = function() {
		request("http://40.81.29.216:8130/transdepth/eth/btc",
				function(error, response, body) {
					if(!error && response.statusCode == 200) {
						console.log("Fuck")
						console.log(body) 
						var f = JSON.parse(body)
						ppomelo.Msg(app, "rolelist", uid, "dataMessage", f);
						//ppomelo.Msg(app, "rolelist", uid, "dataMessage", ret);
					}
				})
	}
	console.log("we starth the user " + uid)
	coinpush.AddTimer(uid , "transdepth", setInterval(c, 5 * 1000));

}
function mytransdepth(app, uid, para)  
{
	var ret = {"type":"mytransdepth"};
	var c = function() {
		ppomelo.Msg(app, "rolelist", uid, "dataMessage", ret);
	}
	coinpush.AddTimer(uid , "mytransdepth", setInterval(c, 5 * 1000));

}
function lasttrade(app, uid, para)  
{
	var ret = {
		"type":"lasttrade", 
		"data":{
			"time":Date.parse(new Date()),   
			"price":"8.88",     
			"count":"88.88",     
			"type": "0 "
		}
	
	};
	var c = function() {
		ppomelo.Msg(app, "rolelist", uid, "lasttrade", ret);
	}
	coinpush.AddTimer(uid , "lasttrade", setInterval(c, 5 * 1000));

}
/**
 * New client entry.
 *
 * @param  {Object}   msg     request message
 * @param  {Object}   session current session object
 * @param  {Function} next    next step callback
 * @return {Void}
 */
Handler.prototype.Register = function(msg, session, next) {
	var rid = session.get('rid');

	var type = msg.type 
	var para = msg.para

	switch(type){
		case "pairlist":
			pairlist(this.app, rid, para) 
			break;
		case "coininfo":
			coininfo(this.app, rid, para) 
			break;
		case "bargraph":
			bargraph(this.app, rid, para) 
			break;
		case "transdepth":
			transdepth(this.app, rid, para) 
			break;
		case "mytransdepth":
			//mytransdepth(this.app, rid, para) 
			break;
		case "lasttrade":
			//lasttrade(this.app, rid, para)
			break;
	}
  next(null, {code: 200, msg: 'game server is ok.'});
};


Handler.prototype.UnRegister = function(msg, session, next) {
	var rid = session.get('rid');
	var type = msg.type
	//暂时什么都没有实现, 回头再增加
	switch(type) {
		case "pairlist":
			break;
		case "coininfo":
			break;
		case "bargraph":
			break;
		case "transdepth": 
			break;
		case "mytransdepth":
			break;
		case "lasttrade":
			break;
	}
  next(null, {code: 200, msg: 'game server is ok.'});
};
